"""文章分类API"""
import json
from typing import List

from fastapi import APIRouter, Depends, HTTPException, status
from fastapi.responses import JSONResponse

from sqlalchemy.orm import Session

from schemas.schema_newstype import NewsTypeOut
from soulapi.db import get_db
from soulapi.schemas import schema_newstype
from soulapi.crud import crud_newstype

router_newstype = APIRouter()


@router_newstype.get('/list/', summary='获取新闻分类列表',
                     description='/newstypes/?skip=0&limit=5, 无参数返回所有结果')
def read_newstypes(skip: int = 0, limit: int = 5, db: Session = Depends(get_db)):
    newstypes = crud_newstype.get_newstypes(db, skip=skip, limit=limit)
    print(newstypes)
    # newstypes 返回的一定是一个列表,当没有查询到数据时候返回的是空列表，这里判断的判断依据不能是 None
    if not newstypes:
        raise HTTPException(status_code=400, detail="没有数据啦，你还查！！！")
    # todo 定义 newstypes的 响应schemas
    response_model = schema_newstype.NewsTypeOut.parse_obj({
        "code": 1001,
        "message": "",
        "detail": newstypes
    })
    return JSONResponse(content=response_model.dict(), status_code=200)


@router_newstype.get('/{nety_id}', response_model=schema_newstype.NewsTypeRead, summary="根据（nety_id）文章分类ID取详情信息",
                     description='如/newstype/心理人物')
def read_newstype(id: int, db: Session = Depends(get_db)):
    db_newstype = crud_newstype.get_newstype(db, id=id)
    if db_newstype is None:
        raise HTTPException(status_code=400, detail=f"{id}<——>分类ID错误或者不存在！！")
    return JSONResponse(content=db_newstype.to_dict(), status_code=200)


@router_newstype.get('/{nety_name}', response_model=schema_newstype.NewsTypeRead, summary="根据（nety_name）文章分类名获取详情信息",
                     description='如/newstype/心理人物')
def read_newstype_by_name(name: str, db: Session = Depends(get_db)):
    db_newstype = crud_newstype.get_newstype_by_name(db, name=name)
    if db_newstype is None:
        raise HTTPException(status_code=400, detail=f"{name}<——>分类名称错误或者不存在！！")
    return JSONResponse(content=db_newstype.to_dict(), status_code=200)


@router_newstype.post('/create/', response_model=schema_newstype.NewsTypeRead, summary="新增文章分类信息",
                      description='json格式以post方式提交')
def create_newstype(newstype: schema_newstype.NewsTypeCreate, db: Session = Depends(get_db)):
    db_newstype = crud_newstype.get_newstype_by_name(db, name=newstype.nety_name)
    if db_newstype:
        raise HTTPException(status_code=400, detail=f"{newstype.nety_name}<——>已经存在了！！")
    return_data = crud_newstype.create_newstype(db=db, newstype=newstype)
    return JSONResponse(content=return_data, status_code=status.HTTP_200_OK)


@router_newstype.post('/update/', response_model=schema_newstype.NewsTypeRead, summary="根据nety_name（文章分类名）更新已有的文章分类信息",
                      description='json格式以post方式提交')
def update_newstype(newstype: schema_newstype.NewsTypeUpdate, db: Session = Depends(get_db)):
    db_newstype = crud_newstype.get_newstype_by_name(db, name=newstype.nety_name)
    if db_newstype is None:
        raise HTTPException(status_code=400, detail=f"{newstype.nety_name}<——>分类名称错误或者不存在！！")
    return_date = crud_newstype.update_newstype(db, newstype=newstype)
    return JSONResponse(content=return_date, status_code=200)


@router_newstype.post('/delete/{nety_id}', summary="根据nety_id（文章分类ID）删除文章分类", description='根据nety_id（文章分类ID）删除信息')
def del_newstype(id: int, db: Session = Depends(get_db)):
    db_newstype = crud_newstype.get_newstype(db, id)
    if db_newstype is None:
        raise HTTPException(status_code=400, detail=f"{id}<——>号文章分类不存在！！")
    return crud_newstype.delete_newstype(db, id).to_dict()


@router_newstype.post('/delete/{nety_name}', summary="根据nety_name（文章分类名）删除文章分类", description='根据nety_name（文章分类名）删除信息')
def del_newstype_by_name(name: str, db: Session = Depends(get_db)):
    db_newstype = crud_newstype.get_newstype_by_name(db, name)
    if db_newstype is None:
        raise HTTPException(status_code=400, detail=f"{name}<——>分类名称错误或者不存在！！")
    return crud_newstype.delete_newstype_by_name(db, name)


@router_newstype.post('/newstype/create/', response_model=schema_newstype.NewsTypeRead, summary="新增文章分类",
                      description='json格式以post方式提交')
def create_newstype(newstype: schema_newstype.NewsTypeCreate, db: Session = Depends(get_db)):
    db_newstype = crud_newstype.get_newstype_by_name(db, nety_name=newstype.nety_name)
    if db_newstype:
        raise HTTPException(status_code=400, detail=f"{newstype.nety_name}<——>已经存在了！！")
    return_data = crud_newstype.create_newstype(db=db, newstype=newstype)
    return JSONResponse(content=return_data, status_code=status.HTTP_200_OK)


@router_newstype.get('/newstype/{nety_name}', response_model=schema_newstype.NewsTypeRead, summary="根据文章分类名获取详情信息",
                     description='如/newstype/心理人物')
def read_newstype(name: str, db: Session = Depends(get_db)):
    db_newstype = crud_newstype.get_newstype_by_name(db, nety_name=name)
    if db_newstype is None:
        raise HTTPException(status_code=400, detail=f"{name}<——>分类名称错误或者不存在！！")
    return db_newstype
